package com.hwtx.form.util;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrUtil {

    private static final Pattern UNDERLINE_PATTERN = Pattern.compile("_(\\w)");

    public static Map<String, String> split(String input, String delimiter, String kvDelimiter) {
        Map<String, String> orderSources = new HashMap<>();
        String[] pairs = input.split(kvDelimiter);

        // 遍历每个键值对并解析
        for (String pair : pairs) {
            String[] keyValue = pair.split(delimiter);
            if (keyValue.length == 2) {
                orderSources.put(keyValue[0].trim(), keyValue[1].trim());
            }
        }
        return orderSources;
    }

    public static String toCamelCase(String underlineName) {
        if (StringUtils.isBlank(underlineName)) {
            return underlineName;
        }
        Matcher matcher = UNDERLINE_PATTERN.matcher(underlineName);
        StringBuilder result = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(result, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(result);
        return result.toString();
    }
}
